package com.changgou.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.content.feign.ContentFeign;
import com.changgou.content.pojo.Content;
import com.changgou.entity.Result;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * @ClassName CanalDataEnventListener
 * @Description 广告监听
 * @Author 洛尘
 * @Date 21:31 2019/11/14
 * @Version 2.1
 **/
@CanalEventListener
public class ContentEventListener {

    @Autowired(required = false)
    private ContentFeign contentFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return
     * @author ShortDust
     * @Description 自定义监听器
     * @ClassName CanalEnventListener
     * @date 2019/11/14 21:56
     */
    @ListenPoint(destination = "example", schema = "changgou_content", table = {"tb_content", "tb_content_category"}
            , eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT})
    public void onEnventContent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        String categoryId = getColumValue(rowData, "category_id");
        //根据id查找广告列表
        Result<List<Content>> result = contentFeign.findById(Long.parseLong(categoryId));
        //存入redis中
        stringRedisTemplate.boundValueOps("content_" + categoryId).set(JSON.toJSONString(result.getData()));
    }

    /**
     * @return
     * @author ShortDust
     * @Description 自定义监听器
     * @ClassName CanalEnventListener
     * @date 2019/11/14 21:56
     */
    @ListenPoint(destination = "example", schema = "changgou_content", table = {"tb_content", "tb_content_category"}
            , eventType = {CanalEntry.EventType.DELETE})
    public void onContentDelete(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //获取对应的值
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            //获取categoryId
            if (column.getName().equals("category_id")) {
                String categoryId = column.getValue();
                Result<List<Content>> result = contentFeign.findById(Long.parseLong(categoryId));
                stringRedisTemplate.boundValueOps("content_" + categoryId).set(JSON.toJSONString(result.getData()));
            }
        }
    }

    //获取对应的值
    private String getColumValue(CanalEntry.RowData rowData, String columName) {
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            if (columName.equals(column.getName())) {
                return column.getValue();
            }
        }
        return null;
    }

}
